package edu.hawaii.myisern.action;

import edu.hawaii.myisern.model.User;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.validation.Validate;

/**
 * Allows a user to log in and access the Web site.  This class was adapted from the Bugzooky
 * example.
 *
 * @author Tim Fennell
 * @author Laura Matsuo
 */
public class LoginActionBean extends MyIsernActionBean {
  @Validate(required = true)
  private String username;

  @Validate(required = true)
  private String password;

  private String targetUrl;

  /** 
   * The username of the user trying to log in. 
   * @param username a String.
   */
  public void setUsername(String username) {
    this.username = username; 
  }

  /** 
   * The username of the user trying to log in. 
   * @return a String.
   */
  public String getUsername() { 
    return username; 
  }

  /** 
   * The password of the user trying to log in. 
   * @param password a String.
   */
  public void setPassword(String password) {
    this.password = password; 
  }

  /** 
   * The password of the user trying to log in. 
   * @return a String.
   */
  public String getPassword() { 
    return password; 
  }

  /**
   * The URL the user was trying to access (null if the login page was accessed directly). 
   * @return a String.
   */
  public String getTargetUrl() { 
    return targetUrl; 
  }

  /** 
   * The URL the user was trying to access (null if the login page was accessed directly). 
   * @param targetUrl a String.
   */
  public void setTargetUrl(String targetUrl) {
    this.targetUrl = targetUrl; 
  }

  /**
   * Logs the user into the system if they have to correct username and password.
   * 
   * @return A Resolution indicating to redisplay a proper page.
   */
  public Resolution login() {
    User user = new User("admin", "admin");
    
    if (this.username.equals(user.getUsername()) && this.password.equals(user.getPassword())) {
      getContext().getRequest().getSession().setAttribute("user", this.username);
      return new RedirectResolution("/myisern.jsp");
    }
    else {
      return new RedirectResolution("/index.jsp");
    }
  }
}
